package com.rzx.mybackend.controller.game;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.MessageProperties;
import com.rzx.mybackend.common.PageResult;
import com.rzx.mybackend.common.Result;
import com.rzx.mybackend.dto.GamePageDTO;
import com.rzx.mybackend.entity.Game;
import com.rzx.mybackend.service.GameService;
import com.rzx.mybackend.utils.RabbitMQUtils;
import com.rzx.mybackend.vo.GameDetailsVO;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * GameController
 *
 * @author renzhexian
 * @date 2025/7/16
 */
@RestController
@RequestMapping("/game")
@RequiredArgsConstructor
public class GameController {

    private static final Logger log = LoggerFactory.getLogger(GameController.class);

    private final GameService gameService;

    /**
     * 分页查询游戏列表
     * @param gamePageDTO
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult> pageGameList(GamePageDTO gamePageDTO) {

        log.info("开始分页查询游戏列表");

        //分页方法
        PageResult pageResult = gameService.pageGameList(gamePageDTO);

        return Result.success(pageResult);
    }

    /**
     * 上传游戏图片
     * @param gameId
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadPicture")
    public Result uploadPicture(Long gameId, @RequestParam(value = "file") MultipartFile multipartFile) throws IOException {

        log.info("开始上传游戏图片");

        gameService.updateGamePicture(gameId, multipartFile);

        return Result.success();
    }

    /**
     * 添加游戏
     * @param game
     * @return
     */
    @PostMapping
    public Result addGame(@RequestBody Game game) {

        gameService.handleDelayAddGame(game);

        return Result.success();
    }

    /**
     * 上传游戏视频
     * @param gameId
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadVideo")
    public Result uploadVideo(Long gameId,@RequestParam(value = "file") MultipartFile multipartFile) throws IOException {

        log.info("开始上传游戏视频，游戏ID: {}", gameId);

        gameService.updateGameVideo(gameId, multipartFile);

        return Result.success();
    }

    /**
     * 根据游戏ID获取游戏信息
     * @param gameId
     * @return
     */
    @GetMapping("/getGameById")
    public Result<Game> getGameById(Long gameId) {

        log.info("开始查询游戏，游戏ID: {}", gameId);

        Game game = gameService.getGameById(gameId);

        return Result.success(game);
    }

    /**
     * 更新游戏信息
     * @param game
     * @return
     */
    @PutMapping("/update")
    public Result updateGame(@RequestBody Game game) {

        log.info("开始更新游戏信息: {}", game);

        gameService.updateGame(game);

        return Result.success();
    }

    /**
     * 根据游戏ID删除游戏
     * @param gameId
     * @return
     */
    @DeleteMapping("/delete/{gameId}")
    public Result deleteGame(@PathVariable Long gameId) {

        log.info("开始删除游戏，游戏ID: {}", gameId);

        gameService.deleteGame(gameId);

        return Result.success();
    }

    /**
     * 根据游戏ID查询游戏详情
     * @param gameId
     * @return
     */
    @GetMapping("/getGameDetails/{gameId}")
    public Result<GameDetailsVO> getGameDetails(@PathVariable Long gameId) {

        log.info("开始查询游戏详情，游戏ID: {}", gameId);

        return Result.success(gameService.getGameDetails(gameId));
    }
}
